# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=pylint
pkgbase=mingw-w64-python3-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.4.3
pkgrel=1
pkgdesc="Analyzes Python code looking for bugs and signs of poor quality (mingw-w64)"
arch=('any')
url='https://pylint.org/'
license=('GPL')
depends=("${MINGW_PACKAGE_PREFIX}-python3-astroid"
         "${MINGW_PACKAGE_PREFIX}-python3-colorama"
         "${MINGW_PACKAGE_PREFIX}-python3-mccabe" 
         "${MINGW_PACKAGE_PREFIX}-python3-isort")
makedepends=("${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-astroid"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-six")
checkdepends=("${MINGW_PACKAGE_PREFIX}-mpdecimal"
              "${MINGW_PACKAGE_PREFIX}-python3-pytest-runner")
optdepends=("${MINGW_PACKAGE_PREFIX}-tk: Pylint GUI"
            "${MINGW_PACKAGE_PREFIX}-graphviz: To have other output formats than dot or vcg")
options=('staticlibs' 'strip' '!debug')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/PyCQA/pylint/archive/pylint-${pkgver}.tar.gz")
sha512sums=('ab1bcb7e51d0e6afd9f20e928bc93f75464b3b370e5dadb17f52090a4cab6639c904d063617bc076ce5e65143432cd7e41e36075a8df213237bfea7ac8a67b0a')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"
 
  sed -i 's/pytest<3.3/pytest/' pylint-pylint-$pkgver/setup.py
  pushd "${_realname}-${_realname}-${pkgver}"
    sed -e "s|/usr/bin/env python|/usr/bin/env python3|" -e "s|/usr/bin/python$|/usr/bin/python3|" -i pylint/epylint.py pylint/__main__.py tests/input/noext tests/data/ascript
  popd
  rm -rf python3-build-${CARCH} | true
  cp -r "${_realname}-${_realname}-${pkgver}" "python3-build-${CARCH}"
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
}

# Note that build() is sometimes skipped because it's done in 
# the packages setup.py install for simplicity if you can do so.
# but sometimes, you want to do a check before install which would
# also trigger the build.
build() {
  msg "Python 3 build for ${CARCH}"  
  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py build
}

check() {
  msg "Python 3 test for ${CARCH}"
  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py pytest || warning "tests failed"
}


package() {
  #This package install is needed for .fixups with .EXE's 
  #in the bit directory.  The install files "python-exe-installs"
  #and should be renamed to your _realname
  install=${_realname}3-${CARCH}.install

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"

# This entire section should be removed if the package does NOT install
# anything in the /mingw*/bin directory. 
### begin section ###
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}
